/*
 * TransactionLineBO.java
 *
 * Created on March 5, 2007, 4:19 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.jjenkins.data;

 /**
 * The class is represents the transactionLine business object. It is created
 * by the transactionLineDAO. It extends businessObject which contains the ID,
 * alreadyInDatabase and dirty. This class contains the attributes of the transaction
 * lines with all getters and setters and any functionality of the object. 
 * 
 * @author      Jeff Jenkins
 * @version     1.0
 * 
 */
public class TransactionLineBO extends BusinessObject{
    
    private String revenueSource = null;
    private String coupon = null;
    private String transaction = null; // I choose to make this just the id, because normally when we want to read a transaction_line alone without first reading the transaction, they would probably not need the transactionBO with it. This is done to save space. If it is need it is read in the get method
    private double subtotal = 0;
    private boolean dirtySubtotal = false;
    
    public TransactionLineBO() {
    }
    
    /*This method returns the revenueSource
     *@return   RevenueSourceBO
     *@throws: dataException if no revenue source exists*/
    
    public RevenueSourceBO getRevenueSource() throws DataException{
        if(revenueSource == null){
            throw new DataException("You must enter a revenue source of a transaction line");
        }else{
            // This gets the revenue source type then it gets teh revenue source dao then it reads
            return (RevenueSourceFactory.getInstance().getDAO(RevenueSourceDAO.getType(revenueSource))).read(revenueSource);
        }
    }
    
      /**This method is sort of a hash. When an or save is called on Transaction Line,
       * the calling method does not want the whole object, it just wants the id. So
       * this method just returns the id. Also I couln't find a way to have it get the object
       * anyways without requiring additional connections*/
    
    public String getRevenueSourceID() {
        
        return revenueSource;
    }
    
    /**This method sets the revenueSource
     *@par   RevenueSourceBO
     *throws DataException*/
    
    public void setRevenueSource(RevenueSourceBO revenueSource) throws DataException{
        this.revenueSource = revenueSource.getId();
        setDirty(true);
    }
    
    /**This method returns the coupon
     *@return   CouponBO*/
    
    public ConceptualCouponBO getCoupon() throws DataException{
        
        // ToDo: You should also check to see if the coupon applies to the
        // correct revenue source at this point.
        
        if(coupon !=null){
            return ConceptualCouponDAO.getInstance().read(coupon);
            
        }else{
            return null;
        }
    }
    
    
      /**This method is sort of a hash. When an or save is called on Transaction Line,
       * the calling method does not want the whole object, it just wants the id. So
       * this method just returns the id. Also I couln't find a way to have it get the object
       * anyways without requiring additional connections*/
    
    public String getCouponID() {
        
        return coupon;
    }
     /** This method sets the coupon id.
      *  @par   CouponBO
      *  @ throws data exception*/
    
    public void setCoupon(ConceptualCouponBO coupon) throws DataException{
        this.coupon = coupon.getId();
        setDirty(true);
    }
    
    
  /*This method returns the Transaction
   *@return   TransactionBO*/
    public TransactionBO getTransaction() throws DataException{
        
        
        return TransactionDAO.getInstance().read(transaction);
    }
    
  /**This method is sort of a hash. When an or save is called on Transaction Line,
   * the calling method does not want the whole object, it just wants the id. So
   * this method just returns the id. Also I couln't find a way to have it get the object
   * anyways without requiring additional connections*/
    
    public String getTransactionID() {
        
        return transaction;
    }
    
  /**This method sets the Transaction
   *@par   TransactionBO*/
    public void setTransaction(TransactionBO transaction) {
        this.transaction = transaction.getId();
        setDirty(true);
    }
    
    
  /**This method calculates and returns the subTotal
   *@return   double*/
    public double getSubtotal()throws DataException{
        
        RevenueSourceBO rev = getRevenueSource();
        
        ConceptualCouponBO c = getCoupon();
        double amount;
        
        // If there is a coupon it wil get the amount
        if(coupon != null){
            amount = c.getAmount();
            
        }else{
            amount = 0;
        }
        
        setSubtotal(rev.getPrice()*rev.getQuantity() - amount);
        return subtotal;
    }
    
  /*This method sets the subTotal
   *@par   SubtotalBO*/
    public void setSubtotal(double subtotal) {
        this.subtotal = subtotal;
        setDirty(true);
    }
    
    
    
    
}
